<template>
<div>
  <van-nav-bar
    title="更新昵称"
    left-text="取消"
    right-text="保存"
    left-arrow
    @click-left="$emit('close')"
    @click-right="onClickRight"
  />

  <van-field
    v-model.trim="message"
    type="textarea"
    maxlength="7"
    placeholder="请输入昵称"
    show-word-limit

  />
  </div>
</template>

<script>
import { updateUserInfo } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      message: ''
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  created () {
    this.message = this.value
  },

  methods: {
    async  onClickRight () {
      Toast.loading({
        message: '加载中...',
        forbidClick: true

      })
      if (this.message === '') return Toast('请输入昵称')
      try {
        const res = await updateUserInfo({
          name: this.message

        })
        console.log(res)
        Toast.success('修改成功')
        this.$emit('close')
        this.$emit('input', this.message)
      } catch (err) {
        if (err.response && err.response.status === 409) {
          return Toast.fail('昵称已存在')
        }
        Toast.fail('修改失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
